﻿@using KPIS.GERP.Models.ViewModel
@using KPIS.GERP.Models.ICM
@model IEnumerable<PurchaseRequestItem>

@{
    Layout = "~/Views/Shared/_EmptyLayout.cshtml";
    ViewBag.Title = "รายการขอซื้อ/จ้าง";
    ViewBag.Descr = "รายการขอซื้อ/จ้างที่ทางพัสดุทำการรวบรวมมาจากการแจ้งความประสงค์ฯ";

    int intPrId = ViewBag.PrId;
    decimal? sumAmount = 0;
}

@using (Html.BeginForm("DeleteAll", "PurchaseRequestItem", FormMethod.Post))
{
    <span style="font-size: large; color: blue">@ViewBag.Descr</span>
    <hr/>
    
    @Html.AntiForgeryToken()

    <div class="row-fluid">    
        <div class="block-button-menu-above">
            @*@if (ViewBag.bEditable)
            {
            @Html.ActionLink("เพิ่ม", "Create", new { PrId = intPrId }
            , new { @class="btn btn-small btn-success button-standard-width", role="button" })
            }*@
             @if(ViewBag.EmptyLayout!=true)
             {
                 @Html.ButtonDeleteAll()
             }
        </div>

        @Html.ValidationSummary(true, "", new {@class = "alert alert-error"})

        <div class="row-fluid">
            <div class="table-header span12">
                <div class="span10">
                    @ViewBag.Title
                </div>
                @Html.Partial("_ExportButton")
            </div>
        </div>

        <table id="table" class="table table-striped table-bordered table-hover" aria-describedby="table_info">
            <thead>
                <tr>
                    <th class="center" style="width: 50px;">
                        <!-- Check box delete multiple records column-->
                        <label>
                            <input type="checkbox" />
                            <span class="lbl"></span>
                        </label>
                    </th>
                    <th>
                        แหล่งเงิน
                    </th>
                    <th>
                        รายการพัสดุ/บริการ
                    </th>
                    <th>
                        รายละเอียดรายการ
                    </th>
                    <th>
                        จำนวน
                    </th>
                    <th>
                        ราคาจากแจ้งความประสงค์
                    </th>
                    <th>
                        จำนวนเงินรวม
                    </th>
                    @*<th>
                        @Html.DisplayNameFor(model => model.RecordStatus)
                    </th>
                    <th>
						@KPIS.GERP.Models.Utility.LookUpTypeNameCaption(KPIS.GERP.Models.ICM.IcmMasLookUpType.LookUpType.Priority)
					</th>*@
                    <th class="center" style="width: 80px;">
                        <!-- Empty head column for export buttons -->&nbsp;
                    </th>
                </tr>
            </thead>
            @{
                GERPContext db = new GERPContext();
                var pprItems = db.PrePurchaseRequestItems.Include("ProjectOperationExpense.ProjectExpense.MasBudgetSource").ToList();
            }
            <tbody>
                @foreach (var m in Model ?? new List<PurchaseRequestItem>())
                {
                    <tr>
                        <td class="center">
                            <!-- Check box all for delete multiple records -->
                            <label>
                                <input type="checkbox" name="deleteIds" value="@(m.PrItemId)" />
                                <span class="lbl"></span>
                            </label>
                        </td>
                        <td>
                            @{
                                var pprItem = pprItems.FirstOrDefault(o => o.PrItemId == m.PrItemId);
                                if (pprItem != null &&
                                    pprItem.ProjectOperationExpense != null &&
                                    pprItem.ProjectOperationExpense.ProjectExpense != null &&
                                    pprItem.ProjectOperationExpense.ProjectExpense.MasBudgetSource != null)
                                {
                                    @:@pprItem.ProjectOperationExpense.ProjectExpense.MasBudgetSource.Name
                                }
                            }
                        </td>
                        <td>
                            @if (m.HaveItem ?? true)
                            {
                                var resourceItemName = m != null && m.MasResourceItem != null ? m.MasResourceItem.Name : "";
                                if(ViewBag.EmptyLayout == true)
                                {
                                    @resourceItemName
                                } else
                                {
                                    @Html.ActionLink(string.IsNullOrWhiteSpace(resourceItemName) ? "ไม่ระบุ":resourceItemName, "Details", new { Id = m.PrItemId }, new{@class="red"})
                                }
                            }
                            else
                            {
                                <span class="red">ไม่ระบุพัสดุที่ซื้อ/จ้างทำ</span>
                            }


                            @*@if (ViewBag.EmptyLayout == true)
                            {
                                @m.MasResourceItem.Name
                            }
                            else
                            {
                                @Html.ActionLink(@m.MasResourceItem.Name, "Details", new {Id = m.PrItemId})
                            }*@
                        </td>
                        <td>
                            @Html.DisplayFor(model => m.Descr)
                        </td>
                        <td style="text-align: right">
                            @(m.Qty.HasValue ? m.Qty.Value.ToString("#,##0") : "0") @Html.DisplayFor(model => m.MasResourceItem.MasUnit.Name)
                        </td>
                        <td style="text-align: right">
                            @(m.Price.HasValue ? m.Price.Value.ToString("#,##0.00") : "0")
                        </td>
                        <td style="text-align: right">
                            @(m.Amount.HasValue ? m.Amount.Value.ToString("#,##0.00") : "0")
                             @if (m.Amount.HasValue) {
                                sumAmount = sumAmount + m.Amount;
                            }
                        </td>
                        <td class="td-actions">
                            @if (ViewBag.EmptyLayout != true)
                            {
                                if (ViewBag.bEditable)
                                {
                                    @Html.Partial("_ButtonMenuEachRow", new ExportGrid
                                        {
                                            Id = m.PrItemId,
                                            DetailsUrl = Url.Action("Details") + "/" + m.PrItemId,
                                            EditUrl = Url.Action("Edit") + "/" + m.PrItemId,
                                            DeleteUrl = Url.Action("Delete") + "?Id=" + m.PrItemId + "&PrId=" + m.PrId
                                        })
                                }
                                else
                                {
                                    @Html.Partial("_ButtonMenuEachRow", new ExportGrid
                                        {
                                            Id = m.PrItemId,
                                            DetailsUrl = Url.Action("Details") + "/" + m.PrItemId
                                        })
                                }
                            }
                        </td>
                    </tr>
                }
            </tbody>
        </table>

         <br />
        <div style="font-size:large;color:blue;text-align:right;padding-right:7em;">
            @if (sumAmount.HasValue){
                @Html.Raw("ยอดรวมทั้งหมด = ")
                    @Html.Raw(sumAmount.Value.ToString("#,##0.00"))
            }
            </div>
    </div>
}

@section scripts {
    
	<script type="text/javascript">
		$(function () {
			var oTable1 = $('#table').dataTable({
				"aoColumns": [
                    { "bSortable": false },
                    null,
                    null,
                    null,
                    null,
                    null,
                    { "bSortable": false }
				]
			});

			$('table th input:checkbox').on('click', function () {
				var that = this;
				$(this).closest('table').find('tr > td:first-child input:checkbox')
				.each(function () {
					this.checked = that.checked;
					$(this).closest('tr').toggleClass('selected');
				});

			});

			$('[data-rel=tooltip]').tooltip();

			$("form").submit(function () {
				var currentForm = this;
				bootbox.confirm(function (result) {
					var _result = result;
					if (result) {
						$.ajax({
						    url: '@(Url.Action("DeleteAll", "PurchaseItem"))',
							type: "post",
							dataType: "json",
							data: $(currentForm).serialize(),
							success: function (result) {
								if (result.success) {
									location.reload();
								}
								else {
								}
							}
						});
					}
				})
				return false;

			});

		})

	</script>

}